home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / joe014.zip / JOE014.TAZ / JOE014.tar / asyncsvr3.c < prev    next >
C/C++ Source or Header  |  1992-01-30  |  5KB  |  269 lines

  1. /* Terminal interface for ESIX
  2.    Copyright (C) 1991 Joseph H. Allen
  3.  
  4. This file is part of JOE (Joe's Own Editor)
  5.  
  6. JOE is free software; you can redistribute it and/or modify it under the terms
  7. of the GNU General Public License as published by the Free Software
  8. Foundation; either version 1, or (at your option) any later version. 
  9.  
  10. JOE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE.  See the GNU General Public License for more details.  
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with JOE; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <signal.h>
  20. #include <fcntl.h>
  21. #include <time.h>
  22. #include <sys/param.h>
  23. #include <termio.h>
  24. #include "async.h"
  25.  
  26. #define DIVISOR 12000000
  27. #define TIMES 2
  28.  
  29. static struct termio oldterm;
  30.  
  31. static unsigned char *obuf=0;
  32. static unsigned obufp=0;
  33. static unsigned obufsiz;
  34. static unsigned long ccc;
  35.  
  36. static unsigned speeds[]=
  37. {
  38. B50,50,B75,75,B110,110,B134,134,B150,150,B200,200,B300,300,B600,600,B1200,1200,
  39. B1800,1800,B2400,2400,B4800,4800,B9600,9600,EXTA,19200,EXTB,38400
  40. };
  41.  
  42. void tsignal();
  43.  
  44. sigjoe()
  45. {
  46. signal(SIGHUP,tsignal);
  47. signal(SIGTERM,tsignal);
  48. signal(SIGINT,SIG_IGN);
  49. signal(SIGPIPE,SIG_IGN);
  50. signal(SIGQUIT,SIG_IGN);
  51. }
  52.  
  53. signorm()
  54. {
  55. signal(SIGHUP,SIG_DFL);
  56. signal(SIGTERM,SIG_DFL);
  57. signal(SIGINT,SIG_DFL);
  58. signal(SIGPIPE,SIG_DFL);
  59. signal(SIGQUIT,SIG_DFL);
  60. }
  61.  
  62. aopen()
  63. {
  64. int x;
  65. struct termio newterm;
  66. fflush(stdout);
  67. ioctl(fileno(stdin),TCGETA,&oldterm);
  68. newterm=oldterm;
  69. newterm.c_lflag=0;
  70. newterm.c_iflag&=~(ICRNL|IGNCR|INLCR);
  71. newterm.c_oflag=0;
  72. newterm.c_cc[VMIN]=1;
  73. newterm.c_cc[VTIME]=0;
  74. ioctl(fileno(stdin),TCSETAW,&newterm);
  75. ccc=0;
  76. for(x=0;x!=30;x+=2)
  77.  if((newterm.c_cflag&CBAUD)==speeds[x])
  78.   {
  79.   ccc=DIVISOR/speeds[x+1];
  80.   break;
  81.   }
  82. if(obuf) free(obuf);
  83. if(!(TIMES*ccc)) obufsiz=4096;
  84. else
  85.  {
  86.  obufsiz=1000000/(TIMES*ccc);
  87.  if(obufsiz>4096) obufsiz=4096;
  88.  }
  89. if(!obufsiz) obufsiz=1;
  90. obuf=(unsigned char *)malloc(obufsiz);
  91. }
  92.  
  93. aclose()
  94. {
  95. aflush();
  96. ioctl(fileno(stdin),TCSETAW,&oldterm);
  97. }
  98.  
  99. int have=0;
  100. static unsigned char havec;
  101. static int yep;
  102.  
  103. static dosig()
  104. {
  105. yep=1;
  106. }
  107.  
  108. aflush()
  109. {
  110. if(obufp)
  111.  {
  112.  write(fileno(stdout),obuf,obufp);
  113.  obufp=0;
  114.  }
  115. if(!have)
  116.  {
  117.  fcntl(fileno(stdin),F_SETFL,O_NDELAY);
  118.  if(read(fileno(stdin),&havec,1)==1) have=1;
  119.  fcntl(fileno(stdin),F_SETFL,0);
  120.  }
  121. }
  122.  
  123. unsigned char *take=0;
  124.  
  125. anext()
  126. {
  127. if(take)
  128.  if(*take)
  129.   {
  130.   int c;
  131.   if(*take!='\\') return *take++;
  132.   ++take;
  133.   if(!*take) return '\\';
  134.   else if(*take=='r') c='\r';
  135.   else if(*take=='b') c=8;
  136.   else if(*take=='n') c=10;
  137.   else if(*take=='f') c=12;
  138.   else if(*take=='a') c=7;
  139.   else if(*take=='\"') c='\"';
  140.   else if(*take>='0' && *take<='7')
  141.         {
  142.         c= *take++-'0';
  143.         if(*take>='0' && *take<='7')
  144.          {
  145.          c=c*8+*take++-'0';
  146.          if(*take>='0' && *take<='7') c=c*8+*take++-'0';
  147.          }
  148.         --take;
  149.         }
  150.   else c= *take;
  151.   ++take;
  152.   return c;
  153.   }
  154.  else take=0;
  155. aflush();
  156. if(have) have=0;
  157. else if(read(fileno(stdin),&havec,1)<1) tsignal(0);
  158. if(record) macroadd(havec);
  159. return havec;
  160. }
  161.  
  162. eputc(c)
  163. unsigned char c;
  164. {
  165. obuf[obufp++]=c;
  166. if(obufp==obufsiz) aflush();
  167. }
  168.  
  169. eputs(s)
  170. char *s;
  171. {
  172. while(*s)
  173.  {
  174.  obuf[obufp++]= *(s++);
  175.  if(obufp==obufsiz) aflush();
  176.  }
  177. }
  178.  
  179. getsize()
  180. {
  181. #ifdef TIOCGSIZE
  182. struct ttysize getit;
  183. #else
  184. #ifdef TIOCGWINSZ
  185. struct winsize getit;
  186. #else
  187. char *p;
  188. #endif
  189. #endif
  190. #ifdef TIOCGSIZE
  191. if(ioctl(fileno(stdout),TIOCGSIZE,&getit)!= -1)
  192.  {
  193.  if(getit.ts_lines>=3) height=getit.ts_lines;
  194.  if(getit.ts_cols>=2) width=getit.ts_cols;
  195.  }
  196. #else
  197. #ifdef TIOCGWINSZ
  198. if(ioctl(fileno(stdout),TIOCGWINSZ,&getit)!= -1)
  199.  {
  200.  if(getit.ws_row>=3) height=getit.ws_row;
  201.  if(getit.ws_col>=2) width=getit.ws_col;
  202.  }
  203. #else
  204. if(p=getenv("ROWS")) sscanf(p,"%d",&height);
  205. if(p=getenv("COLS")) sscanf(p,"%d",&width);
  206. if(height<3) height=24;
  207. if(width<2) width=80;
  208. #endif
  209. #endif
  210. }
  211.  
  212. termtype()
  213. {
  214. unsigned char entry[1024];
  215. unsigned char area[1024];
  216. unsigned char *foo=area;
  217. unsigned char *x=(unsigned char *)getenv("TERM");
  218. if(!x) goto down;
  219. if(tgetent(entry,x)!=1) goto down;
  220. height=tgetnum("li");
  221. if(height<3) height=24;
  222. width=tgetnum("co");
  223. if(width<2) width=80;
  224. if(!tgetstr("cs",&foo)) scroll=0;
  225. down:
  226. getsize();
  227. }
  228.  
  229. shell()
  230. {
  231. int x;
  232. char *s=(char *)getenv("SHELL");
  233. if(!s)
  234.  {
  235.  puts("\nSHELL variable not set");
  236.  return;
  237.  }
  238. eputs("\nYou are at the command shell.  Type 'exit' to continue editing\r\n");
  239. aclose();
  240. if(x=fork())
  241.  {
  242.  if(x!= -1) wait(0);
  243.  }
  244. else
  245.  {
  246.  signorm();
  247.  execl(s,s,0);
  248.  _exit(0);
  249.  }
  250. aopen();
  251. }
  252.  
  253. susp()
  254. {
  255. #ifdef SIGCONT
  256. eputs("\nThe editor has been suspended.  Type 'fg' to continue editing\r\n");
  257. yep=0;
  258. aclose();
  259. signal(SIGCONT,dosig);
  260. sigsetmask(sigmask(SIGCONT));
  261. kill(0,SIGTSTP);
  262. while(!yep) sigpause(0);
  263. signal(SIGCONT,SIG_DFL);
  264. aopen();
  265. #else
  266. shell();
  267. #endif
  268. }
  269.